package com.game.autoTool.project.web.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.game.autoTool.project.web.common.exception.CustomException;
import com.game.autoTool.project.web.domain.entity.GameFunctionItems;
import com.game.autoTool.project.web.domain.entity.GameFunctionOperateFlow;
import com.game.autoTool.project.web.domain.entity.ScreenshotOcr;
import com.game.autoTool.project.web.domain.req.CommonScreenshotReq;
import com.game.autoTool.project.web.domain.req.GetFunctionFlowReq;
import com.game.autoTool.project.web.domain.resp.GameFunctionOperateFlowResp;
import com.game.autoTool.project.web.domain.resp.ScreenshotOcrResp;
import com.game.autoTool.project.web.service.GameFunctionItemsService;
import com.game.autoTool.project.web.service.GameFunctionOperateFlowService;
import com.game.autoTool.project.web.service.GameFunctionService;
import com.game.autoTool.project.web.service.ScreenshotOcrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class GameFunctionServiceImpl implements GameFunctionService {

    @Autowired
    private ScreenshotOcrService screenshotOcrService;
    @Autowired
    private GameFunctionItemsService gameFunctionItemsService;
    @Autowired
    private GameFunctionOperateFlowService gameFunctionOperateFlowService;

    @Override
    public List<GameFunctionOperateFlowResp> getFunctionFlow(GetFunctionFlowReq req) {

        //查询游戏
        GameFunctionItems gameFunctionItems = gameFunctionItemsService.selectByPk(req.getFunctionItemsId());
        if (ObjectUtil.isEmpty(gameFunctionItems)) {
            throw new CustomException("未找到此功能集合!");
        }

        List<GameFunctionOperateFlow> flowList = gameFunctionOperateFlowService.selectByFunctionItemId(gameFunctionItems.getId());

        if (CollectionUtil.isEmpty(flowList)) {
            throw new CustomException("流程不存在!");
        }

        //查询此活动下所有的截图
        List<ScreenshotOcr> screenshotOcrList = screenshotOcrService.selectByGameItemId(gameFunctionItems.getGameItemId());
        Map<Long, ScreenshotOcr> screenshotOcrMap = screenshotOcrList.stream().collect(Collectors.toMap(ScreenshotOcr::getId, Function.identity()));

        //将数据构建好 越精简越好
        List<GameFunctionOperateFlowResp> respList = new ArrayList<>();
        for (GameFunctionOperateFlow flow : flowList) {
            GameFunctionOperateFlowResp resp = new GameFunctionOperateFlowResp();
            resp.setId(flow.getId());
            resp.setNextId(flow.getNextId());
            resp.setOperateType(flow.getOperateType());
            resp.setTargetX(flow.getTargetX());
            resp.setTargetY(flow.getTargetY());
            resp.setStayTime(flow.getStayTime());
            resp.setIsNeedScreenshot(2);
            resp.setKeyInputCode(flow.getKeyInputCode());

            if (ObjectUtil.isNotEmpty(flow.getScreenshotId())) {
                ScreenshotOcr screenshotOcr = screenshotOcrMap.get(flow.getScreenshotId());
                if (ObjectUtil.isEmpty(screenshotOcr)) {
                    System.out.println("未找到 " + screenshotOcr.getId() + " 截图");
                    throw new CustomException("获取异常");
                }

                resp.setIsNeedScreenshot(1);

                ScreenshotOcrResp screenshotOcrResp = new ScreenshotOcrResp();
                screenshotOcrResp.setId(screenshotOcr.getId());
                screenshotOcrResp.setScreenshotCode(screenshotOcr.getScreenshotCode());
                screenshotOcrResp.setExpectedResult(screenshotOcr.getExpectedResult());
                screenshotOcrResp.setPointX(screenshotOcr.getPointX());
                screenshotOcrResp.setPointY(screenshotOcr.getPointY());

                resp.setScreenshotOcrResp(screenshotOcrResp);
            }

            respList.add(resp);

        }

        return respList;
    }

    @Override
    public List<ScreenshotOcrResp> getCommonScreenshot(CommonScreenshotReq req) {
        List<ScreenshotOcr> screenshotOcrList = screenshotOcrService.selectCommonByGameItemId(req.getGameItemId());

        List<ScreenshotOcrResp> respList = new ArrayList<>();
        for (ScreenshotOcr screenshotOcr : screenshotOcrList) {
            ScreenshotOcrResp screenshotOcrResp = new ScreenshotOcrResp();
            screenshotOcrResp.setId(screenshotOcr.getId());
            screenshotOcrResp.setScreenshotCode(screenshotOcr.getScreenshotCode());
            screenshotOcrResp.setExpectedResult(screenshotOcr.getExpectedResult());
            screenshotOcrResp.setPointX(screenshotOcr.getPointX());
            screenshotOcrResp.setPointY(screenshotOcr.getPointY());

            respList.add(screenshotOcrResp);
        }

        return respList;
    }
}

